<?php
class Member
{
	/**
	 * Returns one or all members in the database.
	 * 
	 * @param number Member SSO, NULL to return ALL members
	 * @return mixed Associative array
	 */
	public static function get($sso=null)
	{
		$conn = DB::getConn();
		$result = array();
		
		if (!$sso)
		{
			$sql = "SELECT * FROM member ORDER BY sso";
			$result = $conn->GetAssoc($sql);
		}
		else
		{
			$sql = "SELECT * FROM member WHERE sso = {$sso} ORDER BY sso";
			$result = $conn->GetRow($sql);
		}
		
		return $result;
	}
	
	/**
	 * 
	 */
	public static function add($sso, $is, $firstName, $lastName, $emailSofttek, $emailGE, $birthday, $startingDate, $ext="", $unknown="")
	{
		if (!$sso || !$is || !$firstName || !$lastName || !$emailSofttek || !$emailGE || !$birthday || !$startingDate)
			return "Incomplete Data";
		
		$conn = DB::getConn();
		
		if ($conn->GetOne("SELECT COUNT(*) FROM member WHERE sso = {$sso}"))
			return "SSO already exist";
			
		$sql = "INSERT INTO member SET sso = ?, `is` = ?, firstName = ?, lastName = ?, emailSofttek = ?, emailGE = ?, birthday = ?, startingDate = ?, ext = ?, unknown = ?";
		$params = array($sso, $is, $firstName, $lastName, $emailSofttek, $emailGE, $birthday, $startingDate, $ext, $unknown);

		if ($conn->Execute($sql, $params))
			return self::get($sso);
		else
			return $conn->ErrorMsg();
	}
	
	public static function update($sso, $is, $firstName, $lastName, $emailSofttek, $emailGE, $birthday, $startingDate, $ext="", $unknown="")
	{
		if (!$sso || !$is || !$firstName || !$lastName || !$emailSofttek || !$emailGE || !$birthday || !$startingDate)
			return "Incomplete Data";
		
		$conn = DB::getConn();
		
		$sql = "UPDATE member SET `is` = ?, firstName = ?, lastName = ?, emailSofttek = ?, emailGE = ?, birthday = ?, startingDate = ?, ext = ?, unknown = ? WHERE sso = ?";
		$params = array($is, $firstName, $lastName, $emailSofttek, $emailGE, $birthday, $startingDate, $ext, $unknown, $sso);

		if ($conn->Execute($sql, $params))
			return self::get($sso);
		else
			return $conn->ErrorMsg();
	}
	
	public static function delete($sso)
	{
		if (!$sso)
			return false;
		
		$conn = DB::getConn();
		
		$sql = "DELETE FROM member WHERE sso = ?";
		$params = array($sso);
		
		if ($conn->Execute($sql, $params))
			return true;
		else
			return $conn->ErrorMsg();
	}
}